var App = angular.module('App', ['ngAnimate', 'ui.router', 'chieffancypants.loadingBar', 'ngTable', 'ngCookies']);

App.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider', 'cfpLoadingBarProvider', function($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider, cfpLoadingBarProvider) {
    //set router
    $urlRouterProvider.otherwise("/match");
    $stateProvider
        .state('index', {
            url: '/match',
            templateUrl: 'tpl/start.html'
        })
        .state('subject', {
            url: '/match/subject?matchId',
            templateUrl: 'tpl/match.subject.html',
            controller: 'MatchSubjectController'
        })

        /* 迷你马拉松 */
        .state('minipersonal', {
            url: '/match/mini/personal?matchId?index',
            templateUrl: 'tpl/match.score.html',
            controller: 'MatchMiniPersonalController'
        })
        .state('miniteam', {
            url: '/match/mini/team?matchId?index',
            templateUrl: 'tpl/match.min.team.html',
            controller: 'MatchMiniTeamController'
        })

        /* 团队查询 */
        .state('selectindex', {
            url: '/select/match',
            templateUrl: 'tpl/select.start.html'
        })
        .state('selectsubject', {
            url: '/select/match/subject?matchId',
            templateUrl: 'tpl/select.match.subject.html',
            controller: 'MatchSubjectController'
        })
        .state('score', {
            url: '/select/match/score?matchId?subjectId',
            templateUrl: 'tpl/match.score.html',
            controller: 'MatchScoreController'
        })
        .state('person', {
            url: '/select/match/score/person?matchId?subjectId?userId',
            templateUrl: 'tpl/score.person.html',
            controller: 'ScorePersonController'
        })

        /* 个人查询 */
        .state('inquiry', {
            url: '/inquiry?matchId?subjectId',
            templateUrl: 'tpl/score.inquiry.html',
            controller: 'ScoreInquiryController'
        })
        .state('personal', {
            url: '/inquiry/personal?matchId?subjectId?flag',
            templateUrl: 'tpl/score.person.html',
            controller: 'InquiryListController'
        })
        .state('team', {
            url: '/inquiry/team?matchId?subjectId',
            templateUrl: 'tpl/score.team.html',
            controller: 'InquiryTeamController'
        })
        .state('list', {
            url: '/inquiry/team/list?matchId?subjectId?teamId',
            templateUrl: 'tpl/score.team.list.html',
            controller: 'InquiryTeamListController'
        })
        .state('listPerson', {
            url: '/inquiry/team/list/person?matchId?subjectId?userId',
            templateUrl: 'tpl/score.person.html',
            controller: 'InquiryTeamPersonController'
        });
    // $locationProvider.html5Mode(true);
    
    // true is the default, but I left this here as an example:
    // cfpLoadingBarProvider.includeSpinner = true;
    // cfpLoadingBarProvider.includeBar = false;
    // cfpLoadingBarProvider.spinnerTemplate = '<div class="test"><div id="loading-bar-spinner"><div class="spinner-icon"></div></div></div>';
    
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        /**
        * The workhorse; converts an object to x-www-form-urlencoded serialization.
        * @param {Object} obj
        * @return {String}
        */
        var param = function(obj) {
            var query = '';
            var name, value, fullSubName, subName, subValue, innerObj, i;

            for(name in obj) {
                value = obj[name];

                if(value instanceof Array) {
                    for(i=0; i<value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                }
                else if (value instanceof Object) {
                    for(subName in value) {
                        subValue = value[subName];
                        fullSubName = name + '[' + subName + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                }
                else if(value !== undefined && value !== null) {
                    query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
                }
            }

            return query.length ? query.substr(0, query.length - 1) : query;
        };

        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];

}]);